/**
 * This software was produced for the U. S. Government
 * under Contract No. FA8721-09-C-0002, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation Clause
 * (DFARS) 252.227-7014 (JUN 1995)
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 *
 * SPMIF to OPNET Translator
 * Author: ewalters
 * Created: 1:58:42 PM, Aug 3, 2007
 * MITRE Corporation
 */
package com.mitre.spmif2opnet.analysis;

import com.mitre.spmif2opnet.analysis.ast.Project;
import com.mitre.spmif2opnet.analysis.ast.Graph;


/**
 * Symbol representing reference to an encapsulated graph
 */
public class ExpandedSymbol extends Symbol {

	/**
	 * The graph we will point to
	 */
	private Graph _graph;
	
	/** 
	 * Default constructor
	 * @param id the identifier of this symbol
	 */
	public ExpandedSymbol(Identifier id) {
		super(id);
		_graph = null;
	}
	
	/** 
	 * graph accessor
	 * @return the graph
	 */
	public Graph getGraph() {
		return _graph;
	}
	
	/** 
	 * graph mutator
	 * @param graph the graph
	 */
	public void setGraph(Graph graph) {
		_graph = graph;
	}
	
	/**
	 * Has the symbol been resolved, i.e. do we know what graph we point to?
	 */
	public boolean isResolved() {
		return (_graph != null);
	}

	/**
	 * Resolve the symbol using the given analysis context
	 * Important info for an expansion: graph, name (project and scen s.b. also valid)
	 * NOTE: We assume this means this graph is in the source project and scenario!
	 *  Therefore, the project should also be loaded!
	 * @param c the context
	 */
	public void resolve(AnalysisContext c) {
		if (!this.isResolved()) {
			Project p = c.getProject(this.getId().getProjectName());
			if (p == null) {
				System.out.println("[ERROR] [ExpandedSymbol.resolve] " + this.getId().toString() + " referencing unloaded project!");
				System.exit(-1);
			}
			try {
				Graph g = p.getTask(this.getId());
				this.setGraph(g);				
			} catch (AnalysisException exc) {
				System.out.println("[ERROR] [ExpandedSymbol.resolve] " + exc.getMessage());
				System.exit(-1);
			}
		}
	}
}
